#import "PreferredCharset.h"
#import <objc/objc-runtime.h>

@class ComposeBackEnd;

IMP orgSetPreferredEncoding;
id setPreferredEncoding(id self, SEL _cmd, int encoding)
{
    id ret = orgSetPreferredEncoding(self, _cmd, encoding);
    NSLog(@"setPreferredEncoding: %p", encoding);
    return ret;
}

IMP orgInit;
id init(id self, SEL _cmd)
{
    id ret = orgInit(self, _cmd);
    //[self setPreferredEncoding: 0x820]; // ISO-2022-JP
    [self setPreferredEncoding: 0x8000100];  // UTF-8
    return ret;
}

@implementation PreferredCharset
+ (void) initialize 
{
	[super initialize];
    [PreferredCharset registerBundle];
	NSLog(@"PreferredCharset initialized.");

    Method setPreferredEncodingMethod = class_getInstanceMethod([ComposeBackEnd class],  @selector(setPreferredEncoding:));
    orgSetPreferredEncoding = setPreferredEncodingMethod->method_imp;
    setPreferredEncodingMethod->method_imp = setPreferredEncoding;

    Method initMethod = class_getInstanceMethod([ComposeBackEnd class],  @selector(init));
    orgInit = initMethod->method_imp;
    initMethod->method_imp = init;
}

@end
